Vectors and Matrix Operations
Numbers, Vectors, Matrices
Recall that the set of real numbers is and that a vector, , is just an -tuple of numbers. Similarly, an matrix is just a table of numbers, with rows and columns and we can write
Note that a vector is normally considered equivalent to a matrix i.e. we view these as column vectors.
Examples
In R, a vector can be generated with:
> X <- 3:6
> X
[1] 3 4 5 6
A matrix can be generated in R as follows,
> matrix(X)
[,1]
[1,] 3
[2,] 4
[3,] 5
[4,] 6
We note that R distinguishes between vectors and matrices.
Elementary Operations
We can define multiplication of a real number and a vector by . The sum of two vectors in , and is defined as the vector . We can define multiplication of a number and a matrix and the sum of two matrices (of the same sizes) similarly.
Examples
> A <- matrix(c(1,2,3,4), nr=2, nc=2)
> A
[,1] [,2]
[1,] 1 3
[2,] 2 4
> B <- matrix(c(1,0,2,1), nr=2, nc=2)
> B
[,1] [,2]
[1,] 1 2
[2,] 0 1
> A+B
[,1] [,2]
[1,] 2 5
[2,] 2 5
The Tranpose of a Matrix
In R, matrices may be constructed using the matrix
function and the transpose of , , may be obtained in R by using the t
function:
> A <- matrix(1:6, nrow=3)
> t(A)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
Details
If is an matrix with element in row and column , then or is the matrix with element in row and column .
Examples
Consider a vector in R
> x <- 1:4
> x
[1] 1 2 3 4
> t(x)
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
> matrix(x)
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
> t(matrix(x))
[,1] [,2] [,3] [,4]
The first solution gives a matrix and the second solution gives a matrix.
Matrix Multiplication
Matrices and can be multiplied together if is an matrix and is an matrix. The general element of , , is found by pairing the row of with the column of , and computing the sum of products of the paired terms.
Details
Matrices and can be multiplied together if is a matrix and is a matrix. Given the general element of matrix, is found by pairing the row of with the column of , and computing the sum of products of the paired terms.
Examples
> A <- matrix(c(1,3,5,2,4,6),3,2)
> A
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
> B <- matrix(c(1,1,2,3),2,2)
> B
[,1] [,2]
[1,] 1 2
[2,] 1 3
> A%*%B
[,1] [,2]
[1,] 3 8
[2,] 7 18
[3,] 11 28
More on Matrix Multiplication
Let , , and be , , and matrices, respectively. Then we have
In general, matrix multiplication is not commutative, that is .
We also have
In particular, , when is a column vector
More obvious are the rules
where and when the dimensions of the matrices fit.
Linear Equations
Details
General linear equations can be written in the form .
Examples
The set of equations
can be written in matrix formulation as
i.e. for an appropriate choice of and .
The Unit Matrix
The matrix
is the identity matrix. This is because if a matrix is
then and
The Inverse of a Matrix
If is an matrix and is a matrix such that
then is said to be the inverse of , written
Note that if is an matrix for which an inverse exists, then the equation can be solved and the solution is .
Examples
If matrix is:
then is: